package com.yq.cp.controller.basics;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.RecentUse;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.permission.Permission;
import com.yq.cp.entity.permission.User;
import com.yq.cp.service.basics.RecentUseService;
import com.yq.cp.service.permission.PermissionService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 最近使用表 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/recentUse")
public class RecentUseController {
    @Resource
    RecentUseService recentUseService;
    @Resource
    PermissionService permissionService;
    @PostMapping("save")
    public ApiResult add(@RequestBody RecentUse recentUse){
        User user = UserThreadLocal.get();
        recentUse.setUserId(user.getId());
        recentUseService.saveOrUpdate(recentUse);
        return ApiResult.success();
    }

    @PostMapping("list")
    public ApiResult list(){
//        List<RecentUse> list = recentUseService.list(new QueryWrapper<RecentUse>()
//                .eq("user_id", UserThreadLocal.get().getId()).orderByDesc("create_time")
//                .last("limit 10"));
//        ArrayList<Permission> permissions = new ArrayList<>();
//        if (!list.isEmpty()){
//            for (RecentUse recentUse : list) {
//                Permission byId = permissionService.getById(recentUse.getPermissionId());
//                permissions.add(byId);
//            }
//        }
        List<Permission> permissions = permissionService.selList();
        return ApiResult.success(permissions);
    }

}

